package com.atguigu.yygh.hosp.controler;

import com.aliyuncs.utils.StringUtils;
import com.atguigu.yygh.cmn.common.util.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;

import com.atguigu.yygh.cmn.common.result.Result;
import com.atguigu.yygh.model.hosp.HospitalSet;

import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;


@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;
    @ApiOperation(value = "查询所有医院")
    @GetMapping("findAll")
    public Result<List<HospitalSet>> findAllHospitalSet()
    {
        List<HospitalSet> list = hospitalSetService.list();
        return  Result.ok(list);
    }
    @ApiOperation(value = "根据id删除医院")
    @DeleteMapping("{id}")
    public  Result removeHospSet(@PathVariable long id)
    {
        boolean b = hospitalSetService.removeById(id);
        return  b==true?Result.ok("删除成功"):Result.fail("删除失败");
    }
    @ApiOperation(value = "分页查询")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result  findPageHospSet(@PathVariable int current ,
                                   @PathVariable int limit,
                                   @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo)
    {
        System.out.println("接受的数据:"+hospitalSetQueryVo);
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        if(hospitalSetQueryVo ==null){
            System.out.println("分页查询没有固定查询");
            Page<HospitalSet> page1 = hospitalSetService.page(page);
            return Result.ok(page1);
        }
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();//医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();//医院编号
        if(!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        if(!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }
        //调用方法实现分页查询
        IPage<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);
        //返回结果
        return Result.ok(pageHospitalSet);
    }

    @ApiOperation(value = "添加医院接口")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody(required = false) HospitalSet hospitalSet){
//        设置状态
        hospitalSet.setStatus(1);
//        设置签名密钥
        Random random=new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        System.out.println("数据:---------");
        System.out.println(hospitalSet);
        boolean save = hospitalSetService.save(hospitalSet);
        return save?Result.ok():Result.fail();
    }

    @ApiOperation(value = "根据id获取医院信息")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        HospitalSet byId = hospitalSetService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation(value = "修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody(required = true) HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);
        return b?Result.ok():Result.fail();
    }
    @ApiOperation(value = "批量删除")
    @DeleteMapping
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        boolean b = hospitalSetService.removeByIds(idList);
        return b?Result.ok():Result.fail();
    }

    @ApiOperation(value = "修改医院状态")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id, @PathVariable Integer status) {
        HospitalSet hospitalSetServiceById = hospitalSetService.getById(id);
        hospitalSetServiceById.setStatus(status);
        boolean b = hospitalSetService.updateById(hospitalSetServiceById);
        return b?Result.ok():Result.fail();
    }
    @ApiOperation(value = "发送签名密钥")
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable Long id){
        HospitalSet hospitalSetServiceById = hospitalSetService.getById(id);
        String signKey = hospitalSetServiceById.getSignKey();
        String hoscode = hospitalSetServiceById.getHoscode();
//        发送短信
        return Result.ok();
    }
}
